package server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public abstract class DB {
    private static final String name="com.mysql.cj.jdbc.Driver";
    private static final String url ="jdbc:mysql://localhost:3306/?serverTimezone=GMT%2B8";
    private static final String user="root";
    private static final String password="yht990905";
    protected Connection con;
    protected String DBName;
    protected String currentTable;
    protected String varStr;

    public DB(final String DBName, final String tableName, final String varStr) {
        try{
            Class.forName(name);
            con = DriverManager.getConnection(url, user, password);
            createDB(DBName);
            this.currentTable = tableName;
            this.varStr = varStr;
            createTab(tableName, varStr);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    private void createDB(final String DBName) throws SQLException {
        final String checkDB = "select count(*) from information_schema.schemata WHERE schema_name=\"" + DBName + "\"";
        final String createDB = "create database " + DBName;
        final Statement st = con.createStatement();
        final ResultSet rSet = st.executeQuery(checkDB);
        if (rSet.next() && rSet.getInt(1) != 0) {
            //System.out.println("database " + DBName + " exists.");
        } else {
            st.executeUpdate(createDB);
           //System.out.println("database " + DBName + " has created.");
        }
        st.executeUpdate("use " + DBName);
        st.close();
        this.DBName = DBName;
    }
    public void createTab(final String table, final String varStr) throws SQLException {
        final String checkT = "select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_NAME=\"" + table + "\"";
        final String createT = "create table " + table + varStr;
        final Statement st = con.createStatement();
        final ResultSet rSet = st.executeQuery(checkT);
        if (rSet.next()) {
            //System.out.println("table " + table + " exists.");
        } else {
            st.executeUpdate(createT);
            //System.out.println("table " + table + " has created.");
        }
        st.close();
        currentTable = table;
    }
    public boolean delAll(){
        try {
            final String sql = "drop database " + DBName;
            final Statement st = con.createStatement();
            st.executeUpdate(sql);
            st.close();
            return true;
        }catch(SQLException e){
            return false;
        }
    }
    public void close(){
        try{
            if(con!=null) con.close();
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
    public abstract boolean add(final Object o);
    public abstract boolean update(final Object o1, final Object o2);
    public abstract boolean delete(final Object o);
    public abstract Object query(final Object o);
    public List<String> queryTable() throws SQLException {
        final String sql="select table_name from information_schema.tables where table_schema='"+DBName+"'";
        final Statement st = con.createStatement();
        final ResultSet rSet = st.executeQuery(sql);
        List<String> ret = new ArrayList<String>();
        while(rSet.next()){
            ret.add(rSet.getString(1));
        }
        return ret;
    }

}